package com.lgb.controller;

import com.lgb.Service.BlogService;
import com.lgb.Service.TypeService;
import com.lgb.pojo.Type;
import com.lgb.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;


import java.util.List;

@Controller
public class TypeShowController {

    @Autowired
    private TypeService typeService;

    @Autowired
    private BlogService blogService;

    //跳转到分类页面
    @GetMapping("/types/{id}")
    public String types(@PageableDefault(size = 5 , sort = {"updateTime"} ,direction = Sort.Direction.DESC)
                                   @PathVariable Long id, Pageable pageable, Model model){
        //获取全部的分类
        List<Type> types = typeService.listTypeTop(10000);
        if(id == -1){
            //博客首页进入，默认选取第一个分类
            id = types.get(0).getId();
        };
        //类别
        model.addAttribute("types" , types);
        BlogQuery blogQuery = new BlogQuery();
        blogQuery.setTypeId(id);
        //博客
        model.addAttribute("page" , blogService.listBlog(pageable,blogQuery));
        model.addAttribute("activeTypeId" , id);
        return "types";
    }
}
